Skip to content

Leidy Sphinx Task List API#43

Open
Leidy-Martinez wants to merge 6 commits intoAda-C22:mainfrom
Leidy-Martinez:main
Open

Leidy Sphinx Task List API#43
Leidy-Martinez wants to merge 6 commits intoAda-C22:mainfrom
Leidy-Martinez:main

Conversation

@Leidy-Martinez
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown

@mikellewade mikellewade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work, Leidy! Please feel free to reach out if you have any questions about the comments left.

Comment thread app/__init__.py
app = Flask(__name__)

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://postgres:postgres@localhost:5432/task_list_api_development'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would want to make sure to delete a comment like this so you don't expose your database connection string.

Comment thread app/models/goal.py
from .task import Task

class Goal(db.Model):
__tablename__ = 'goal'
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would automatically be set to goal, is there a reason as to why use this syntax here?

Comment thread app/models/goal.py

if tasks_ids:
tasks_ids_list = [task.id for task in self.tasks]
goal_dict["task_ids"] = tasks_ids_list
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have an endpoint that will provide us with the same data that goal_to_dict["tasks"] will give us so we don't need to put it on the dictionary representation of the object. If I client has the information to retrieve a Goal (i.e. goal.id) then they have the information to get the same information from our endpoint.

Comment thread app/models/goal.py
Comment on lines +34 to +35
if include_name:
return {Goal.__name__.lower(): goal_dict}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! We could even make this more general and maybe add it to the Base class since we do this for both Task and Goal.

Comment thread app/models/goal.py
class Goal(db.Model):
__tablename__ = 'goal'
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
title: Mapped[str] = mapped_column(nullable=False)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want a column to be nullable then we would want to use Optional like you did on line 11. Without Optional we don't need nullable=False.

Comment thread app/routes/task_routes.py
Comment on lines +28 to +43
query = db.select(Task) #select records from db Model

# Check for sorting parameter and apply
sorting_param = request.args.get("sort", "asc")

if sorting_param == "desc":
query = query.order_by(Task.title.desc())
else:
query = query.order_by(Task.title)

#query = query.order_by(Task.id)#select records
tasks = db.session.scalars(query) #retrieve the records

response =[]
for task in tasks:
response.append(task.to_dict(include_name=False))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this sorting logic be placed inside of our get_models_with_filters function?

Comment thread app/routes/task_routes.py
Comment on lines +70 to +90
@bp.patch("/<task_id>/<task_status>")
def task_status(task_id, task_status):

task = validate_model(Task,task_id) #record with id = task_id

# Update task status based on the task_status value
if task_status == "mark_complete":
task.completed_at = datetime.now()


# Send Slack notification
send_slack_message(f"Someone just completed the task '{task.title}'")

elif task_status == "mark_incomplete":
task.completed_at = None # Set to None to indicate incomplete
else:
# Return error response for invalid task_status
return {"error": "Invalid task status provided"}, 400

db.session.commit() #save the changes to db
return task.to_dict()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work here! ⭐️

Comment thread tests/test_task_model.py
assert len(result["task"]) == 4
assert result["task"]["id"] == 1
assert result["task"]["title"] == "Test Task"
assert result["task"]["description"] is None No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on unit testing the models, this way you are able to make sure that the models work appropriately before integrating them into other parts of our code base.

Comment thread tests/test_wave_03.py
Comment on lines +155 to +163
def test_invalid_task_status(client,one_task):
# Act
response = client.patch('/tasks/1/invalid_status')
response_data = response.get_json()

# Assert that the status code is 400 (Bad Request)
assert response.status_code == 400
assert len(response_data) == 1
assert response_data == {"error": "Invalid task status provided"}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⭐️

Comment thread tests/test_wave_05.py
Comment on lines +92 to +108
response = client.put("/goals/1", json={
"title": "Updated Goal Title"
})
response_body = response.get_json()

# Assert
# ---- Complete Assertions Here ----
# assertion 1 goes here
# assertion 2 goes here
# assertion 3 goes here
assert response.status_code == 200
assert "goal" in response_body
assert response_body == {
"goal": {
"id": 1,
"title": "Updated Goal Title"
}
}
goal = db.session.get(Goal, 1) # Use Session.get() for SQLAlchemy 2.0 compatibility
assert goal.title == "Updated Goal Title"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants